package com.smrtcold.dev.plc;

import com.github.xingshuangs.iot.protocol.s7.enums.EPlcType;
import com.github.xingshuangs.iot.protocol.s7.service.S7PLC;

public class S7Test3 {
    S7PLC   plc ;

    public static void main(String[] args) throws Exception {
        S7PLC plc = new S7PLC(EPlcType.S200_SMART,"192.168.2.100" );
        plc.hotRestart();
        plc.coldRestart();
        plc.readFloat32("VD1038");
        plc.plcStop();

        System.err.println( plc.readFloat32("VD1038"));

    }

    private static void extracted(S7PLC plc, String addres) {
        System.err.println("read:" + addres + "=========================");
        try {
            System.err.println("int16:" + plc.readInt16(addres));
        } catch (Exception e) {
        }
        try {
            System.err.println("int32:" + plc.readInt32(addres));//
        } catch (Exception e) {
        }
        try {
            System.err.println("folt32:" + plc.readFloat32(addres));
        } catch (Exception e) {
        }
        try {
            System.err.println("folt64:" + plc.readFloat64(addres));
        } catch (Exception e) {
        }
        try {
            System.err.println("bool:" + plc.readBoolean(addres));
        } catch (Exception e) {

        }
        try {
            System.err.println("string:" + plc.readString(addres));
        } catch (Exception e) {

        }
    }


    public Number readAddres(String addres) {

        String _suf = addres.split(" ")[0];
        int i = _suf.lastIndexOf(".");
        if (i > 0) {
            _suf = _suf.substring(i + 1);
            addres = addres.replaceAll("." + _suf, "");
        }
        try {
            switch (_suf) {
                case "VD":
                    return this.plc.readFloat32(addres);//ReadUInt32  ReadInt16  ReadDouble}
                case "VW":
                case "LW":
                    return this.plc.readInt16(addres);
                case "M":
                case "B":
                case "Q":
                case "V":

                    return this.plc.readBoolean(addres) ? 1 : 0;

                case "SRW":
                    return null;
                default:
                    // log.warn("未找到：{}", point);
                    return null;
            }
        } catch (Exception e) {
            System.err.println("error:" + e.getMessage());
            return null;
        }
    }

}
